package loc.dd.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import loc.dd.service.ProcessService;
import loc.dd.service.ProjectService;
import net.sf.json.JSONObject;
import loc.dd.entity.Progress;
/**
*@author 作者：huyaqi
*@version 创作时间:2023年12月5日下午1:50:52
*
*/
@RestController
@RequestMapping("/process")
public class ProcessController {
private static Logger logger =LoggerFactory.getLogger(ProcessService.class);
	
	@Autowired
	private ProcessService processService;

	@Autowired
	private ProjectService projectService;
	
    @Value("${file.download.dir}")
    private String fileUploadPath;
	
    @CrossOrigin
	@RequestMapping(value = "edit" , method = RequestMethod.POST)
	public Map<String,Object> login(@RequestBody JSONObject params,HttpServletRequest request,HttpServletResponse response) {
		JSONObject jsonx = (JSONObject) params.get("data");
		Map<String, Object> map = new HashMap<>();
		Progress process =new Progress();
		System.out.println(jsonx);
		String oid=jsonx.get("oid").toString();
		String procesTime=jsonx.get("procesTime").toString();
		String abilityname=jsonx.get("abilityname").toString();
		String proid = processService.getabilitynameID(abilityname);
//		String procesAbility=proid;
		String procesCreater=jsonx.get("procesCreater").toString();
		String procesInfo=jsonx.get("procesInfo").toString();
		net.sf.json.JSONArray procesFiles2 = jsonx.getJSONArray("procesFiles");
		String procesFiles = procesFiles2.toString().replaceAll("\"", "");
		procesFiles=procesFiles.replace("[", "").replace("]","");
		System.out.println(procesFiles);
		procesFiles = (procesFiles != null && !procesFiles.isEmpty()) ? procesFiles : "暂无附件";
		process.setOid(oid);
		process.setProcesAbility(proid);
		process.setProcesCreater(procesCreater);
		process.setProcesFiles(procesFiles);
		process.setProcesInfo(procesInfo);
		process.setProcesTime(procesTime);
		process.setStatus("1");
		try {
			Map<String, Object> progress= new HashMap<String, Object>();
			if("" != jsonx.get("id") && !"".equals(jsonx.get("id")) && jsonx.get("id") !=null){
				process.setId((int)jsonx.get("id"));
				processService.editProgress(process);
				progress = processService.getmessage(process.getId());
				String progressFiles = (String) progress.get("procesFiles");
				if (!"".equals(progressFiles) && progressFiles != null) {
				    String[] fileArray = progressFiles.split(",");
				    List<Map<String, String>> fileList = new ArrayList<>();
				    for (String fileName : fileArray) {
				        Map<String, String> fileMap = new HashMap<>();
//				        fileMap.put("fullPath", "172.16.0.202:8081/project/download/");
				        fileMap.put("fullPath", fileUploadPath);
				        fileMap.put("name", fileName);
				        fileList.add(fileMap);
				    }
				    System.out.println("fileList="+fileList);
				    progress.put("procesFiles", fileList);
				}
				projectService.updatestatus(1,Integer.parseInt(progress.get("oid").toString()));
				map.put("status", "200");
				map.put("process", progress);
				return map;
			}else {
				processService.addProgress(process);
				progress = processService.getmessage(process.getId());
				String progressFiles = (String) progress.get("procesFiles");
				projectService.updatestatus(1,Integer.valueOf(process.getOid()));
				if (!"".equals(progressFiles) && progressFiles != null) {
				    String[] fileArray = progressFiles.split(",");
				    List<Map<String, String>> fileList = new ArrayList<>();
				    for (String fileName : fileArray) {
				        Map<String, String> fileMap = new HashMap<>();
//				        fileMap.put("fullPath", "172.16.0.202:8081/project/download/");
				        fileMap.put("fullPath", fileUploadPath);
				        fileMap.put("name", fileName);
				        fileList.add(fileMap);
				    }
				    System.out.println("fileList="+fileList);
				    progress.put("processfile", fileList);
				}
				map.put("status", "200");
				map.put("process", progress);
				return map;
			}
			
		}catch (Exception e) {
		    // 处理其他异常类型
		    e.printStackTrace(); // 或者进行其他处理
		    map.put("status", "407");
			return map;
		}
		}
	
}
